package com.aidong.web.controller.box;

/**
 * Created by john on 16-11-3.
 */
import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.BoxCodeDao;
import com.aidong.dao.wills.BoxRentDao;
import com.aidong.model.wills.BoxCode;
import com.aidong.model.wills.BoxRent;
import com.aidong.model.wills.MemberEarnest;
import com.aidong.model.wills.MemberInfo;
import com.aidong.model.wills.PayList;
import com.aidong.web.controller.member.PayController;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"boxRent"})
public class BoxRentController extends PayController {
    @Resource
    BoxRentDao boxRentDao;
    @Resource
    BoxCodeDao boxCodeDao;

    public BoxRentController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("box/rent/list")).addObject("useType", this.getTypeList("boxUseType"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "name",required = false) String name, @RequestParam(value = "useType",required = false) String useType, @RequestParam(value = "status",required = false) Integer status, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        cd.eq("c.shopCode", this.getLoginShopCode());
        cd.eq("useType", useType);
        if(!this.chkNull(status)) {
            if(status.intValue() == 1) {
                cd.isNull("r.memberCode");
            }

            if(status.intValue() == 2) {
                cd.isNotNull("r.memberCode");
            }
        }

        if(!this.chkNull(name)) {
            ConditionMap mv = new ConditionMap(1);
            mv.like("c.code", "%" + name + "%");
            mv.like("c.chName", "%" + name + "%");
            cd.addSub(mv);
        }

        if(!this.chkNull(dateFrom)) {
            cd.ge("r.endTime", this.parseDate(dateFrom, "yyyy-MM-dd"));
        }

        if(!this.chkNull(dateTo)) {
            cd.le("r.endTime", this.parseDate(dateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
        }

        this.boxRentDao.queryRentPageList(pg, cd);
        ModelAndView mv1 = new ModelAndView("box/rent/_list");
        mv1.addObject("list", pg.getResultList());
        mv1.addObject("pg", pg);
        return mv1;
    }

    @RequestMapping({"add"})
    public ModelAndView add(@RequestParam("code") String code) {
        BoxCode box = this.boxCodeDao.getByCode(this.getLoginShopCode(), code);
        if(box != null && box.getStatus().intValue() == 1) {
            if(!this.boxCodeDao.chkBoxFree(this.getLoginShopCode(), code)) {
                return this.showAlert("租箱号已出租");
            } else {
                ModelAndView mv = new ModelAndView("box/rent/add");
                mv.addObject("box", box);
                mv.addObject("_oper", "add");
                return mv;
            }
        } else {
            return this.showAlert("租箱号错误");
        }
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("rent") BoxRent rent, @RequestParam("payls") String payls) {
        if(this.chkNull(rent.getMemberCode())) {
            return this.getResult(false, "请指定会员");
        } else if(this.chkNull(rent.getBoxCode())) {
            return this.getResult(false, "请选择租箱号");
        } else if(this.chkNull(rent.getBeginTime())) {
            return this.getResult(false, "请指定开始时间");
        } else if(this.chkNullZ(rent.getRentMonth())) {
            return this.getResult(false, "请输入租赁月数");
        } else if(this.chkNull(rent.getFactAmount())) {
            return this.getResult(false, "请输入实收租金");
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(rent.getBeginTime());
            cal.add(2, rent.getRentMonth().intValue());
            rent.setEndTime(cal.getTime());
            rent.setShopCode(this.getLoginShopCode());
            BoxCode box = this.boxCodeDao.getByCode(rent.getShopCode(), rent.getBoxCode());
            if(box != null && box.getStatus().intValue() == 1) {
                if(!this.boxCodeDao.chkBoxFree(rent.getShopCode(), rent.getBoxCode())) {
                    return this.getResult(false, "租箱号已出租");
                } else {
                    rent.setBoxCode(box.getCode());
                    int yy = rent.getRentMonth().intValue() / 12;
                    int mm = rent.getRentMonth().intValue() % 12;
                    rent.setRentAmount(Double.valueOf(box.getYearFee().doubleValue() * (double)yy + box.getMonthFee().doubleValue() * (double)mm));
                    rent.setDepositAmount(box.getDeposit());
                    MemberInfo member = this.memberInfoDao.getByMemberCode(rent.getMemberCode());
                    if(member == null) {
                        return this.getResult(false, "会员信息错误");
                    } else {
                        if(!this.chkNullZ(rent.getEarnestId())) {
                            MemberEarnest payList = (MemberEarnest)this.memberEarnestDao.getById(rent.getEarnestId());
                            if(payList == null || payList.getStatus().intValue() != 0 || !payList.getShopCode().equalsIgnoreCase(this.getLoginShopCode())) {
                                return this.getResult(false, "定金不可用");
                            }

                            rent.setEarnestAmount(payList.getEarnestAmount());
                        } else {
                            rent.setEarnestId((Integer)null);
                            rent.setEarnestAmount(Double.valueOf(0.0D));
                        }

                        rent.setSaleType("1");
                        rent.setSaleDate(new Date());
                        rent.setPayAmount(Double.valueOf(rent.getFactAmount().doubleValue() + rent.getDepositAmount().doubleValue() - rent.getEarnestAmount().doubleValue()));
                        rent.setStatus(Integer.valueOf(this.equals(rent.getRentAmount().doubleValue(), rent.getFactAmount().doubleValue())?1:2));
                        rent.setSignStatus(Integer.valueOf(this.chkNullZ(rent.getSignStatus())?2:0));
                        rent.setPrintLabel(Integer.valueOf(0));
                        rent.setCreateUser(this.getLoginUserCode());
                        rent.setCreateTime(new Date());
                        rent.setUpdateTime(new Date());
                        ArrayList payList1 = new ArrayList();
                        StringBuilder payMsg = new StringBuilder();
                        PayList pay = this.getPayList(payls, payList1, "04", payMsg);
                        if(pay == null) {
                            return this.getResult(false, payMsg.toString());
                        } else if(!this.equals(pay.getAmount().doubleValue(), rent.getPayAmount().doubleValue())) {
                            return this.getResult(false, "付款金额不一致");
                        } else {
                            rent.setSalesCode(pay.getSalesCode());
                            rent.setSalesCode2(pay.getSalesCode2());

                            try {
                                this.boxRentDao.addRent(rent, payList1);
                                this.smsListService.addSmsList("31", rent.getSaleId().intValue());
                                this.addUserLog("新增租箱合同", "销售号:" + rent.getSaleId() + ",会员:" + rent.getMemberCode() + ",箱号:" + rent.getBoxCode());
                            } catch (Exception var12) {
                                this.log.error("add rent", var12);
                                return this.getResult(false, "保存发生错误");
                            }

                            return this.getSuccess(String.valueOf(rent.getSaleId()), "保存完成");
                        }
                    }
                }
            } else {
                return this.getResult(false, "租箱号错误");
            }
        }
    }

    @RequestMapping({"resume"})
    public ModelAndView resume(@RequestParam("code") String code) {
        BoxCode box = this.boxCodeDao.getByCode(this.getLoginShopCode(), code);
        if(box != null && box.getStatus().intValue() == 1) {
            BoxRent rent = this.boxRentDao.getRentByCode(this.getLoginShopCode(), code);
            if(rent == null) {
                return this.showAlert("租箱号未出租");
            } else {
                MemberInfo member = this.memberInfoDao.getByMemberCode(rent.getMemberCode());
                if(member == null) {
                    return this.showAlert("会员信息错误");
                } else {
                    ModelAndView mv = new ModelAndView("box/rent/resume");
                    mv.addObject("box", box);
                    mv.addObject("rent", rent);
                    mv.addObject("member", member);
                    return mv;
                }
            }
        } else {
            return this.showAlert("租箱号错误");
        }
    }

    @RequestMapping({"_resume"})
    @ResponseBody
    public String _resume(@ModelAttribute("rent") BoxRent rent, @RequestParam("payls") String payls) {
        if(this.chkNull(rent.getBoxCode())) {
            return this.getResult(false, "租箱号错误");
        } else if(this.chkNull(rent.getMemberCode())) {
            return this.getResult(false, "会员信息错误");
        } else if(this.chkNull(rent.getBeginTime())) {
            return this.getResult(false, "请指定开始时间");
        } else if(this.chkNullZ(rent.getRentMonth())) {
            return this.getResult(false, "请输入租赁月数");
        } else if(this.chkNull(rent.getFactAmount())) {
            return this.getResult(false, "请输入实收租金");
        } else {
            rent.setShopCode(this.getLoginShopCode());
            BoxCode box = this.boxCodeDao.getByCode(rent.getShopCode(), rent.getBoxCode());
            if(box != null && box.getStatus().intValue() == 1) {
                List list = this.boxRentDao.getRentListByCode(rent.getShopCode(), rent.getBoxCode());
                if(list != null && list.size() != 0) {
                    BoxRent rent1 = (BoxRent)list.get(0);
                    if(!rent1.getMemberCode().equalsIgnoreCase(rent.getMemberCode())) {
                        return this.getResult(false, "会员信息错误");
                    } else {
                        rent.setBeginTime(rent1.getEndTime());
                        Calendar cal = Calendar.getInstance();
                        cal.setTime(rent.getBeginTime());
                        cal.add(2, rent.getRentMonth().intValue());
                        rent.setEndTime(cal.getTime());
                        rent.setBoxCode(box.getCode());
                        int yy = rent.getRentMonth().intValue() / 12;
                        int mm = rent.getRentMonth().intValue() % 12;
                        rent.setRentAmount(Double.valueOf(box.getYearFee().doubleValue() * (double)yy + box.getMonthFee().doubleValue() * (double)mm));
                        rent.setDepositAmount(Double.valueOf(0.0D));
                        MemberInfo member = this.memberInfoDao.getByMemberCode(rent.getMemberCode());
                        if(member == null) {
                            return this.getResult(false, "会员信息错误");
                        } else {
                            if(!this.chkNullZ(rent.getEarnestId())) {
                                MemberEarnest payList = (MemberEarnest)this.memberEarnestDao.getById(rent.getEarnestId());
                                if(payList == null || payList.getStatus().intValue() != 0 || !payList.getShopCode().equalsIgnoreCase(this.getLoginShopCode())) {
                                    return this.getResult(false, "定金不可用");
                                }

                                rent.setEarnestAmount(payList.getEarnestAmount());
                            } else {
                                rent.setEarnestId((Integer)null);
                                rent.setEarnestAmount(Double.valueOf(0.0D));
                            }

                            rent.setSaleType("2");
                            rent.setNote("续租 - " + rent1.getSaleId());
                            rent.setSaleDate(new Date());
                            rent.setPayAmount(Double.valueOf(rent.getFactAmount().doubleValue() + rent.getDepositAmount().doubleValue() - rent.getEarnestAmount().doubleValue()));
                            rent.setStatus(Integer.valueOf(this.equals(rent.getRentAmount().doubleValue(), rent.getFactAmount().doubleValue())?1:2));
                            rent.setSignStatus(Integer.valueOf(this.chkNullZ(rent.getSignStatus())?2:0));
                            rent.setPrintLabel(Integer.valueOf(0));
                            rent.setCreateUser(this.getLoginUserCode());
                            rent.setCreateTime(new Date());
                            rent.setUpdateTime(new Date());
                            ArrayList payList1 = new ArrayList();
                            StringBuilder payMsg = new StringBuilder();
                            PayList pay = this.getPayList(payls, payList1, "04", payMsg);
                            if(pay == null) {
                                return this.getResult(false, payMsg.toString());
                            } else if(!this.equals(pay.getAmount().doubleValue(), rent.getPayAmount().doubleValue())) {
                                return this.getResult(false, "付款金额不一致");
                            } else {
                                rent.setSalesCode(pay.getSalesCode());
                                rent.setSalesCode2(pay.getSalesCode2());

                                try {
                                    this.boxRentDao.addRent(rent, payList1);
                                    this.smsListService.addSmsList("31", rent.getSaleId().intValue());
                                    this.addUserLog("租箱续租", "销售号:" + rent.getSaleId() + ",会员:" + rent.getMemberCode() + ",箱号:" + rent.getBoxCode());
                                } catch (Exception var14) {
                                    this.log.error("add rent", var14);
                                    return this.getResult(false, "保存发生错误");
                                }

                                return this.getSuccess(String.valueOf(rent.getSaleId()), "保存完成");
                            }
                        }
                    }
                } else {
                    return this.getResult(false, "租箱号未出租");
                }
            } else {
                return this.getResult(false, "租箱号错误");
            }
        }
    }

    @RequestMapping({"retur"})
    public ModelAndView retur(@RequestParam("code") String code) {
        BoxCode box = this.boxCodeDao.getByCode(this.getLoginShopCode(), code);
        if(box != null && box.getStatus().intValue() == 1) {
            BoxRent rent = this.boxRentDao.getRentByCode(this.getLoginShopCode(), code);
            if(rent == null) {
                return this.showAlert("租箱号未出租");
            } else {
                MemberInfo member = this.memberInfoDao.getByMemberCode(rent.getMemberCode());
                if(member == null) {
                    return this.showAlert("会员信息错误");
                } else {
                    ModelAndView mv = new ModelAndView("box/rent/return");
                    mv.addObject("box", box);
                    mv.addObject("rent", rent);
                    mv.addObject("member", member);
                    return mv;
                }
            }
        } else {
            return this.showAlert("租箱号错误");
        }
    }

    @RequestMapping({"_retur"})
    @ResponseBody
    public String _retur(@ModelAttribute("rent") BoxRent rent, @RequestParam("payls") String payls) {
        if(this.chkNull(rent.getBoxCode())) {
            return this.getResult(false, "租箱号错误");
        } else if(this.chkNull(rent.getMemberCode())) {
            return this.getResult(false, "会员信息错误");
        } else if(this.chkNull(rent.getDepositAmount())) {
            return this.getResult(false, "押金错误");
        } else if(this.chkNull(rent.getAddAmount())) {
            return this.getResult(false, "请输入补收租金");
        } else {
            rent.setShopCode(this.getLoginShopCode());
            BoxCode box = this.boxCodeDao.getByCode(rent.getShopCode(), rent.getBoxCode());
            if(box != null && box.getStatus().intValue() == 1) {
                List list = this.boxRentDao.getRentListByCode(rent.getShopCode(), rent.getBoxCode());
                if(list != null && list.size() != 0) {
                    double deposit = 0.0D;
                    double addAmount = rent.getAddAmount().doubleValue();
                    rent.setPayAmount(Double.valueOf(rent.getAddAmount().doubleValue() - rent.getDepositAmount().doubleValue()));

                    for(Iterator payList = list.iterator(); payList.hasNext(); addAmount = 0.0D) {
                        BoxRent payMsg = (BoxRent)payList.next();
                        if(!payMsg.getMemberCode().equalsIgnoreCase(rent.getMemberCode())) {
                            return this.getResult(false, "会员信息错误");
                        }

                        payMsg.setReturnDate(new Date());
                        payMsg.setReturnDeposit(payMsg.getDepositAmount());
                        payMsg.setAddAmount(Double.valueOf(addAmount));
                        payMsg.setReturnUser(this.getLoginUserCode());
                        deposit += payMsg.getReturnDeposit().doubleValue();
                    }

                    if(!this.equals(rent.getDepositAmount().doubleValue(), deposit)) {
                        return this.getResult(false, "押金错误");
                    } else {
                        ArrayList payList1 = new ArrayList();
                        StringBuilder payMsg1 = new StringBuilder();
                        PayList pay = this.getPayList(payls, payList1, "04", payMsg1);
                        if(pay == null) {
                            return this.getResult(false, payMsg1.toString());
                        } else if(!this.equals(pay.getAmount().doubleValue(), rent.getPayAmount().doubleValue())) {
                            return this.getResult(false, "付款金额不一致");
                        } else {
                            BoxRent rent1 = (BoxRent)list.get(0);
                            Iterator e = payList1.iterator();

                            while(e.hasNext()) {
                                PayList p = (PayList)e.next();
                                p.setSaleId(rent1.getSaleId());
                                p.setContractCode(String.valueOf(rent1.getSaleId()));
                            }

                            try {
                                this.boxRentDao.updateReturn(list, payList1);
                                this.addUserLog("租箱归还", "会员:" + rent.getMemberCode() + ",箱号:" + rent.getBoxCode());
                            } catch (Exception var15) {
                                this.log.error("return rent", var15);
                                return this.getResult(false, "保存发生错误");
                            }

                            return this.getSuccess(String.valueOf(rent.getSaleId()), "保存完成");
                        }
                    }
                } else {
                    return this.getResult(false, "租箱号未出租");
                }
            } else {
                return this.getResult(false, "租箱号错误");
            }
        }
    }
}